package com.ydy.service.brand.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ydy.constant.SystemConstant;
import com.ydy.exception.BusinessException;
import com.ydy.exception.DataNotFoundException;
import com.ydy.exception.ValidateException;
import com.ydy.ienum.EnumBrand;
import com.ydy.mapper.BrandMapper;
import com.ydy.model.Brand;
import com.ydy.service.brand.BrandService;
import com.ydy.utils.ValidateUtil;
import com.ydy.vo.other.BaseVo;
import com.ydy.vo.other.PageVo;
import com.ydy.vo.other.ResultVo;

import tk.mybatis.mapper.entity.Example;

@Service
@Transactional
public class BrandServiceImpl implements BrandService {

	private final static Logger log = LoggerFactory.getLogger(BrandServiceImpl.class);
	@Autowired
	private BrandMapper brandMapper;

	@Override
	public PageVo<Brand> select(Brand brand, Integer page, Integer size) {
		PageVo<Brand> vo = new PageVo<Brand>(page, size);
		Page<Brand> pageBean = PageHelper.startPage(vo.getPage(), vo.getSize(), "inx asc,id desc");
		List<Brand> list = brandMapper.select(brand);
		vo.setTotal(pageBean.getTotal());
		vo.setList(list);
		return vo;
	}

	@Override
	public PageVo<Brand> list(Brand brand, Integer page, Integer size) {
		PageVo<Brand> vo = new PageVo<Brand>(page, size);
		Page<Brand> pageBean = PageHelper.startPage(vo.getPage(), vo.getSize(), "inx asc,id desc");
		Example example = new Example(Brand.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("useStatus", SystemConstant.USE_STATUS_ON);
		List<Brand> list = brandMapper.selectByExample(example);
		vo.setTotal(pageBean.getTotal());
		vo.setList(list);
		return vo;
	}

	@Override
	public Brand saveOrUpdate(Brand brand) {
		if (brand == null) {
			throw new NullPointerException();
		}
		// 校验数据有效
		Map<String, String> validateInfo = ValidateUtil.validateEntity(brand);
		if (!validateInfo.isEmpty()) {
			throw new ValidateException(validateInfo);
		}
		// 新增信息
		if (brand.getId() == null) {
			brand.setUseStatus(SystemConstant.USE_STATUS_OFF);
			brand.setCreateTime(new Date());
			brandMapper.insertSelective(brand);
			log.info("新增brand成功：" + brand.getId());
		} else {// 根据id更新信息
			Brand temp = brandMapper.selectByPrimaryKey(brand.getId());
			if (temp == null) {
				log.info("找不到brand信息：" + brand.getId());
				throw new DataNotFoundException(EnumBrand.DATA_NOT_FOUND);
			}
			brand.setUpdateTime(new Date());
			brandMapper.updateByPrimaryKeySelective(brand);
			log.info("更新brand成功：" + brand.getId());
		}
		return brand;
	}

	@Override
	public BaseVo delete(Long id) {
		if (id == null) {
			throw new NullPointerException();
		}
		Brand temp = brandMapper.selectByPrimaryKey(id);
		if (temp == null) {
			log.info("找不到brand信息：" + id);
			throw new DataNotFoundException(EnumBrand.DATA_NOT_FOUND);
		}
		brandMapper.deleteByPrimaryKey(id);
		log.info("删除brand成功：" + id);
		return new ResultVo();
	}

	@Override
	public BaseVo status(Long id) {
		if (id == null) {
			throw new NullPointerException();
		}
		Brand temp = brandMapper.selectByPrimaryKey(id);
		if (temp == null) {
			log.info("找不到brand信息：" + id);
			throw new DataNotFoundException(EnumBrand.DATA_NOT_FOUND);
		}
		Brand brand = new Brand();
		brand.setId(id);
		StringBuilder builder = new StringBuilder();
		if (Objects.equals(SystemConstant.USE_STATUS_ON, temp.getUseStatus())) {
			brand.setUseStatus(SystemConstant.USE_STATUS_OFF);
			builder.append("Brand下架成功:").append(id);
		} else {
			brand.setUseStatus(SystemConstant.USE_STATUS_ON);
			builder.append("Brand上架成功:").append(id);
		}
		brandMapper.updateByPrimaryKeySelective(brand);
		log.info(builder.toString());
		return new ResultVo();
	}

	@Override
	public Brand selectById(Long id) {
		if (id == null) {
			throw new NullPointerException();
		}
		Brand temp = brandMapper.selectByPrimaryKey(id);
		if (temp == null) {
			throw new BusinessException(EnumBrand.DATA_NOT_FOUND);
		}
		return temp;
	}

}
